const router = require('koa-router')()
const connection = require('./db'); // 导入数据库连接对象


router.post('/getcoursescore', async (ctx, next) => {
    const courseid = ctx.request.body.courseid;
    const studentid = ctx.request.body.studentid;
    console.log(courseid, studentid);

    // 第一个查询
    const query1 = 'SELECT * FROM `student-course` WHERE courseid = ? AND studentid = ?';
    const query1Promise = new Promise((resolve, reject) => {
        connection.query(query1, [courseid, studentid], (error, results, fields) => {
            if (error) {
                console.error(error);
                reject("查询失败");
            } else {
                resolve(results);
            }
        });
    });

    // 第二个查询
    const query2 = 'SELECT * FROM `student-course` WHERE courseid = ?';
    const query2Promise = new Promise((resolve, reject) => {
        connection.query(query2, courseid, (error, results, fields) => {
            if (error) {
                console.error(error);
                reject("查询失败");
            } else {
                resolve(results);
            }
        });
    });

    // 并行执行两个查询
    try {
        const results1 = await query1Promise;
        const results2 = await query2Promise;
        
        console.log(results1);
        console.log(results2);

        ctx.body = { results1, results2 };
    } catch (err) {
        console.error(err);
        ctx.status = 500;
        ctx.body = "查询失败";
    }
});

router.post('/uploadcoursescore', async (ctx, next) => {
    const { studentid, courseid, value } = ctx.request.body;
    
    // 在这里执行数据库查询和更新操作
    const query3 = 'UPDATE `student-course` SET score = ? WHERE studentid = ? AND courseid = ?';
    await connection.query(query3, [value, studentid, courseid]);
    ctx.body = "修改成功";

    // 可以根据需要添加适当的错误处理或响应返回
});




module.exports = router